package mk.jug.academy.controller;

import java.text.MessageFormat;
import java.util.Properties;
import java.util.logging.Logger;

import mk.jug.academy.factory.EventFactory;
import mk.jug.academy.model.Page;
import mk.jug.academy.model.Sponsor;
import mk.jug.academy.service.EventService;
import mk.jug.academy.service.SponsorService;
import mk.jug.academy.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * View layer coordinator for the {@link Sponsor} related functionality in the backend.
 *
 * @author Ice.Penov
 */
@Controller
public class SponsorAdminController extends BaseController {
	
	private static final Logger LOG = Logger.getLogger(SponsorAdminController.class.getName());
	
	//TODO: Externalize this.
	public static final int itemsPerPage = 8;
	
	@Autowired private SponsorService sponsorService;
	@Autowired private EventService eventService;
	@Autowired private Properties properties;
	
	private static String LIST_PENDING = "admin.sponsors.pending";
	private static String LIST_ACTIVE = "admin.sponsors.active";
	private static String LIST_REJECTED = "admin.sponsors.rejected";
	private static String VIEW_SPONSOR = "admin.sponsors.view";
	
	@RequestMapping("/admin/sponsors/accept.do")
	public String acceptSponsor(final ModelMap map, final @RequestParam("id") Long id, final @RequestParam("redirectURL") String redirectURL) {
		return new ControllerContext(map) {
			public String execute() {
				Sponsor sponsor = sponsorService.load(id);
				sponsorService.activate(sponsor);
				LOG.fine("Sponsor [" + sponsor.getCode() + "] has been approved.");
				eventService.createEvent(EventFactory.acceptSponsorEvent(sponsor));
				String subject = properties.getProperty("activation.sponsor.email.subject");
				String body = MessageFormat.format(properties.getProperty("activation.sponsor.email.body"), new Object[] {sponsor.getDisplayName()});
				Utils.sendMail(new String[] {sponsor.getEmail(), sponsor.getDisplayName()}, subject, body);
				return "redirect:" + redirectURL;
			}
		}.result();
	}
	
	@RequestMapping("/admin/sponsors/reject.do")
	public String rejectSponsor(final ModelMap map, final @RequestParam("id") Long id, final @RequestParam("redirectURL") String redirectURL) {
		return new ControllerContext(map) {
			public String execute() {
				Sponsor sponsor = sponsorService.load(id);
				sponsorService.delete(sponsor);
				LOG.fine("Sponsor [" + sponsor.getCode() + "] has been rejected.");				
				return "redirect:" + redirectURL;
			}
		}.result();
	}
	
	@RequestMapping("/admin/sponsors/view.do")
	public String view(final ModelMap map, final @RequestParam("id") Long id) {
		return new ControllerContext(map) {
			public String execute() {
				Sponsor sponsor = sponsorService.load(id);
				map.addAttribute("sponsor", sponsor);
				return VIEW_SPONSOR;
			}
		}.result();
	}
	
	@RequestMapping("/admin/sponsors/pending.do")
	public String listPending(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p, final @RequestParam(value = "highlightID", required = false) String highlightID) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Sponsor> pagedSponsors = sponsorService.listRegistered(page, itemsPerPage);
				map.addAttribute("sponsors", pagedSponsors.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedSponsors.getTotalCount()));
				map.addAttribute("page", page);
				map.addAttribute("highlightID", highlightID);
				return LIST_PENDING;
			}
		}.result();
	}
	
	@RequestMapping("/admin/sponsors/active.do")
	public String listActive(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p, final @RequestParam(value = "highlightID", required = false) String highlightID) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Sponsor> pagedSponsors = sponsorService.listActive(page, itemsPerPage);
				map.addAttribute("sponsors", pagedSponsors.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedSponsors.getTotalCount()));
				map.addAttribute("page", page);
				map.addAttribute("highlightID", highlightID);
				return LIST_ACTIVE;
			}
		}.result();
	}
	
	@RequestMapping("/admin/sponsors/rejected.do")
	public String listRejected(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p, final @RequestParam(value = "highlightID", required = false) String highlightID) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Sponsor> pagedSponsors = sponsorService.listDeleted(page, itemsPerPage);
				map.addAttribute("sponsors", pagedSponsors.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedSponsors.getTotalCount()));
				map.addAttribute("page", page);
				map.addAttribute("highlightID", highlightID);
				return LIST_REJECTED;
			}
		}.result();
	}

}